package edu.gatech.cc.hwa.server.math;

import java.math.BigInteger;

public class UsefulNumbers {
	
	public static final BigInteger Q = new BigInteger("1374382144263552283354001974614343845122153787749");

	public static BigInteger getRandomBigNumberLessThanQ() {
		BigInteger bigOne;
		while((bigOne = randomPositive160BitInteger1()).compareTo(Q) >= 0) {}
		return bigOne;
	}
	
	public static BigInteger randomPositive160BitInteger() {
		BigInteger two = new BigInteger("2");
		BigInteger val = new BigInteger("2");
		int bits = 160;
		val = val.pow(bits-1);
		for (int i=1;i<(bits-1);i++) {
			if (Math.random() <.5) {
				val = val.add(two.pow(i));
			}
		}
		return val;
	}
	
	private static BigInteger randomPositive160BitInteger1() {
		byte[] byteArray = new byte[21];
		byteArray[0] = 0;
		byteArray[1] = (byte)(-1*randomIntInRange(1, 128));
		for (int i = 2; i<byteArray.length; i++) {
			byteArray[i] = (byte)(-1*randomIntInRange(-128, 127));
		}
		return new BigInteger(byteArray);
	}
	
	private static Integer randomIntInRange(Integer min, Integer max) {
		int range = max+1-min;
		int rand = (int)(Math.random()*range);
		rand+= min;
		return rand;
	}
}
